package cn.fl.insurance.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.insurance.condition.InsTypInfoQueryCondition;
import cn.fl.insurance.dto.InsTypInfoDTO;
import cn.fl.insurance.service.IInsTypInfoApiService;
import cn.fl.insurance.service.IInsTypInfoService;
import cn.fl.insurance.transform.InsTypInfoVOTransform;
import cn.fl.insurance.vo.InsTypInfoVO;

/**
 * @Description: 保险种类基础表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class InsTypInfoServiceImpl implements IInsTypInfoService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(InsTypInfoServiceImpl.class);

    /**
     * TODO 自定义 API 暴露服务
     */
    @Autowired
    private IInsTypInfoApiService insTypInfoApiService;

    @Override
    public InsTypInfoVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        InsTypInfoDTO dto = this.insTypInfoApiService.selectInsTypInfoById(id);
        return InsTypInfoVOTransform.toVO(dto);
    }

    @Override
    public Long add(InsTypInfoVO entity) throws BaseException {
        if (null == entity) return null;
        return this.insTypInfoApiService.addInsTypInfo(InsTypInfoVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.insTypInfoApiService.removeInsTypInfoByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(InsTypInfoVO entity) throws BaseException {
        if (null == entity) return false;
        return this.insTypInfoApiService.modifyInsTypInfo(InsTypInfoVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) page = new Pager();
        page=this.insTypInfoApiService.searchInsTypInfoListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        if(CollectionUtils.isNotEmpty(page.getResultList())){
            page.setResultList(InsTypInfoVOTransform.toVOList((List<InsTypInfoDTO>) page.getResultList()));
        }
        return page;
    }

    @Override
    public List<InsTypInfoVO> searchList(InsTypInfoQueryCondition condition) throws BaseException {
        if (null == condition) condition = new InsTypInfoQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        return InsTypInfoVOTransform.toVOList(this.insTypInfoApiService.searchInsTypInfoList(condition));
    }

    /**
     * 查询险种
     *
     * @param condition
     * @return
     */
    @Override
    public List<KeyValueVO> searchInsTypCdList(InsTypInfoQueryCondition condition) throws BaseException{
        List<InsTypInfoDTO> list = this.insTypInfoApiService.searchInsTypInfoList(condition);
        List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
        if(CollectionUtils.isEmpty(list)){
            return kvList;
        }
        for (InsTypInfoDTO insTypInfoDTO : list) {
            KeyValueVO kv = new KeyValueVO();
            kv.setKey(""+insTypInfoDTO.getId());
            kv.setValue(insTypInfoDTO.getTypNm());
            kvList.add(kv);
        }
        return kvList;
    }


    /**
     * 查询父级险种
     *
     * @param condition
     * @return
     */
    @Override
    public Map<String,String> searchInsTypCdMap(InsTypInfoQueryCondition condition) throws BaseException{
        List<InsTypInfoDTO> list = this.insTypInfoApiService.searchInsTypInfoList(condition);
        Map<String,String> map = null;
        if(CollectionUtils.isEmpty(list)){
            return map;
        }
        map = new HashMap<>(list.size());
        for (InsTypInfoDTO insTypInfoDTO : list) {
            KeyValueVO kv = new KeyValueVO();
            map.put(""+insTypInfoDTO.getId(),insTypInfoDTO.getTypNm());
        }
        return map;
    }
}
